<template>
  <view>
    <view class="add_rule_container">
      <van-field
        :value="bargin_ruleForm.cut_item_title"
        placeholder="请输入自定义名称"
        :border="false"
        @change="change_cut_title"
        type="textarea"
        autosize
        label="自定义名称"
      />

      <van-field
        :value="bargin_ruleForm.price"
        placeholder="请输入商品价格"
        :border="false"
        disabled
        label="商品价格"
      />
      <van-field
        :value="bargin_ruleForm.success_times"
        placeholder="请输入已成功次数"
        :border="false"
        @change="change_suc_times"
        label="成功次数"
      />
      <van-field
        :value="bargin_ruleForm.open_times"
        placeholder="请输入活动被开启次数"
        :border="false"
        @change="change_open_times"
        label="已开启次数"
      />
      <van-cell>
        <view slot="title">{{
          bargin_ruleForm.show_need_times == 1
            ? "显示砍价所需刀数"
            : "不显示所需刀数"
        }}</view>
        <view slot="">
          <van-switch
            :checked="bargin_ruleForm.show_need_times == 1 ? true : false"
            @change="change_show_need_times"
          />
        </view>
      </van-cell>
      <van-cell>
        <view slot="title">{{
          bargin_ruleForm.show_bj_price == 1 ? "显示保底价" : "不显示保底价"
        }}</view>
        <view slot="">
          <van-switch
            :checked="bargin_ruleForm.show_bj_price == 1 ? true : false"
            @change="change_show_bj_price"
          />
        </view>
      </van-cell>
      <view style="padding-left: 30upx">图片(图片大小不超过1m)</view>
      <view class="upload" style="padding-left: 20upx">
        <uImg
          ref="upimg"
          :canUploadFile="true"
          :limit="limit"
          :uploadFileUrl="uploadFileUrl"
          :uImgList.sync="uImgList"
          @uploadSuccess="uploadSuccess"
          :fileKeyName="name"
        />
        <!-- <van-button
          type="primary"
          size="mini"
          style="margin-left: 20upx"
          @tap="upload"
          >确定上传</van-button
        > -->
      </view>
      <div class="btn_box">
        <van-button @tap="submit" size="large" color="#f95a4b">提交</van-button>
        <van-button @tap="cancle_submit" size="large">返回</van-button>
      </div>
    </view>
    <van-dialog id="van-dialog" />
    <van-toast id="van-toast" />
  </view>
</template>

<script>
import uImg from "../../components/zhtx-uploadImg/zhtx-uploadImg";
import { item_save } from "../../utils/imapi";
export default {
  props: {},
  components: {
    uImg,
  },
  data() {
    return {
      bargin_ruleForm: {},
      uImgList: [],
      uploadFileUrl:
        "https://api.comm.t.xingkonglian.net:8098/api/v1/oss/upload_starx",
      limit: 1,
      name: "file",
      editIndex: 0,
    };
  },
  computed: {},
  watch: {},
  onLoad(options) {
    if (options.item) {
      this.bargin_ruleForm = JSON.parse(options.item);
      console.log(this.bargin_ruleForm);
      this.editIndex = options.index;
      this.uImgList.push(JSON.parse(options.item).pos_pic);
    }
  },
  onReady() {},
  onShow() {},
  methods: {
    async submit() {
      console.log(this.bargin_ruleForm);
      if (
        this.bargin_ruleForm.need_times == "" ||
        this.bargin_ruleForm.one_people_times == "" ||
        this.bargin_ruleForm.cut_item_title == "" ||
        this.bargin_ruleForm.pos_pic == "" ||
        this.bargin_ruleForm.price == "" ||
        (this.bargin_ruleForm.open_times == "" &&
          this.bargin_ruleForm.open_times != 0) ||
        (this.bargin_ruleForm.success_times == "" &&
          this.bargin_ruleForm.success_times != 0)
      ) {
        this.$toast("请填写完整数据");
        return;
      }
      this.bargin_ruleForm.hour = Number(this.bargin_ruleForm.hour);
      this.bargin_ruleForm.minutes = Number(this.bargin_ruleForm.minutes);
      this.bargin_ruleForm.need_times = Number(this.bargin_ruleForm.need_times);
      this.bargin_ruleForm.one_people_times = Number(
        this.bargin_ruleForm.one_people_times
      );
      this.bargin_ruleForm.open_times = Number(this.bargin_ruleForm.open_times);
      this.bargin_ruleForm.price = Number(this.bargin_ruleForm.price);
      this.bargin_ruleForm.cut_price = Number(this.bargin_ruleForm.price);
      this.bargin_ruleForm.success_times = Number(
        this.bargin_ruleForm.success_times
      );
      this.bargin_ruleForm.show_need_times = Number(
        this.bargin_ruleForm.show_need_times
      );
      this.bargin_ruleForm.show_bj_price = Number(
        this.bargin_ruleForm.show_bj_price
      );
      this.bargin_ruleForm.limit_radius_pub = Number(
        this.bargin_ruleForm.limit_radius_pub
      );
      try {
        const data = await item_save(this.bargin_ruleForm);
        if (data.code == 0) {
          const timestap = new Date().getTime();
          this.bargin_ruleForm.pos_pic =
            this.bargin_ruleForm.pos_pic + "?" + timestap;
          this.$toast.success("编辑成功");
          let currentPages = getCurrentPages();
          let pages = currentPages[currentPages.length - 2];
          pages.$vm.$set(pages.$vm.goodList, this.editIndex, this.bargin_ruleForm);
          pages.$vm.$forceUpdate();
          //   pages.goodList[this.editIndex]=this.bargin_ruleForm
          uni.navigateBack({
            delta: 1,
          });
          //   if (this.goodList[this.editIndex] == undefined){
          //     this.goodList[this.editIndex] = {}
          //   }
          //   this.goodList[this.editIndex].pos_pic=this.bargin_ruleForm.pos_pic
          //   this.goodList[this.editIndex].cut_item_title=this.bargin_ruleForm.cut_item_title
          //   this.rule_show = false;
          // this.cutprice_list();
        }
      } catch (err) {
        console.log(err);
        this.$toast.success("编辑失败");
      }
    },
    cancle_submit() {
     uni.navigateBack({
          delta: 1
     });
    },
    change_need_times(e) {
      this.bargin_ruleForm.need_times = e.detail;
    },
    change_per_times(e) {
      this.bargin_ruleForm.one_people_times = e.detail;
    },
    change_cut_title(e) {
      this.bargin_ruleForm.cut_item_title = e.detail;
    },
    change_cut_price(e) {
      this.bargin_ruleForm.cut_price = e.detail;
    },
    change_suc_times(e) {
      this.bargin_ruleForm.success_times = e.detail;
    },
    change_open_times(e) {
      this.bargin_ruleForm.open_times = e.detail;
    },
    change_show_need_times(e) {
      console.log(e);
      this.bargin_ruleForm.show_need_times = e.detail ? 1 : 0;
    },
    change_show_bj_price(e) {
      console.log(e);
      this.bargin_ruleForm.show_bj_price = e.detail ? 1 : 0;
    },
    uploadSuccess(result) {
      if (result.statusCode == 200) {
        //上传成功的回调处理
        console.log(result.data);
        const img = JSON.parse(result.data).data;
        console.log(img);
        this.bargin_ruleForm.pos_pic = img;
        console.log(result);
      } else {
      }
    },
  },
  onReachBottom() {},
  onHide() {},
};
</script>

<style scoped lang="scss" scoped>
.btn_box {
  padding: 0 20px;
  margin-top: 20upx;
  /deep/ .van-button:first-child {
    margin-bottom: 20upx;
  }
}
.upload {
  display: flex;
  align-items: center;
}
</style>
